package AOP;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class jdkproxy {
    public static void main(String[] args) {
        //创建接口实现类代理对象
        Class[] interfaces={userDao.class};
        userDaoimp userDaoimp=new userDaoimp();
        userDao o = (userDao)Proxy.newProxyInstance(jdkproxy.class.getClassLoader(), interfaces, new userdaoproxy(userDaoimp));
        System.out.println(o.add(1,2));
    }
}
class userdaoproxy implements InvocationHandler{
    //把需要代理对象的类传递过来，用有参构造传递
    private Object obj;
    public userdaoproxy(Object obj){
        this.obj=obj;
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("方法之前执行");
        Object o = method.invoke(obj, args);
        System.out.println("方法之后执行");
        return o;

    }
}